<!--
 * @Description: 
 * @Author: charles
 * @Date: 2021-07-12 10:56:47
 * @LastEditors: Hzl
 * @LastEditTime: 2021-10-07 13:28:35
-->
<template>
  <div>
    <!-- 头部 -->
    <Header></Header>
    <!-- /头部 -->


    <!-- 体部 -->
    <div class="content">
      <div class="wrapper">
        
        <div class="title">
          {{category.name}}
        </div>
        <div class="inner_content">
          
          <!-- 左侧表格 -->
          <div class="articleList">
            <!-- 文章列表 -->
            <ul v-if='categoryList.total > 0'>
              <li class="list_list" v-for='item in categoryList.list' :key="item.id">
                <div class="artitle" @click="todetails(item.id)">{{item.title}}</div>
                <div class="list">
                  <!-- 发表时间 -->
                  <span>{{item.publishTime | fmtDate}}</span>
                  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                  <!-- 阅读次数 -->
                  <span>{{item.readTimes}}</span>
                </div>
              </li>
              
            </ul>
            <div v-else class="nodata">暂无数据</div>
            <!-- 分页 -->
            <div class="page">
              <el-pagination
                hide-on-single-page
                background
                layout="prev, pager, next"
                @current-change="handleCurrentChange"
                :page-size="categoryList.pageSize"
                :total="categoryList.total">
              </el-pagination>
            </div>
            <!-- /分页 -->
          </div>
          <!-- 右侧热门资讯 -->
          <div class="rank">
            <div class="hot">热门</div>
            <ul class="rank_list">
              <li v-for="item in categoryHotList" :key="item.id" @click="todetails(item.id)"> 
                {{item.title}}
              </li>
            </ul>
          </div>
        </div>
        
      </div>
    </div>
    <!-- /体部 -->

    <!-- 底部 -->
    <Footer></Footer>
    <!-- /底部 -->
  </div>
</template>

<script>
import {get} from '../utils/request'
//1.引入头部组件
import Header from '../components/Header.vue';
//1.引入底部组件
import Footer from '../components/Footer.vue';
export default {
  //注册局部组件
  components:{
    Header,
    Footer
  },
  created(){
    this.category=this.$route.query;
    this.loadArticle();
    this.loadHotArticle();
  },
  //监听器 监听路由里面的query参数  $route.query
  watch:{
    '$route.query':{
      //当属性发生变化触发handler函数
      handler(query){
        //参数发生变化进行重新赋值
        this.category=query;
        //赋值之后再重新加载
        this.loadArticle()
      },
      deep:true
    }
  },
  methods:{
    //给分页按钮绑定事件
    handleCurrentChange(page){
      // alert(page);  //2
      this.params.page=page;  //2
      this.loadArticle();
    },
    //加载文章的方法
    loadArticle(){
      let url = "/index/article/pageQuery"
      this.params.categoryId=this.category.id;
      get(url,this.params).then(res=>{
        this.categoryList=res.data;
      });
    },
    //跳转到详情页
    todetails(id){
      this.$router.push({
        path:"/article",
        query:{id:id}
      })
    },
    //右侧加载热点资讯方法
    loadHotArticle(){
      let url = '/index/article/findHotArticles';
      get(url).then(res=>{
        this.categoryHotList = res.data;
      })
    }
  },
  data(){
    return{
      category:{},
      params:{
        page:1,
        pageSize:5
      },
      categoryList:{list:[]},
      categoryHotList:{list:[]}
    }
  }
}
</script>

<style lang="scss">
  .content{
    padding: 2em .5em;
    background-color: #ecf6f2;
    // height: 500px;
    &>.wrapper{
      .inner_content{
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        & > div{
          background-color: #fff;
          height: 500px;
          padding: 1em .5em;
          border-radius: 5px;
        }
        .articleList{
          flex: 1;
          margin-right: 2em;
          ul > li.list_list{
            &>div{
              line-height: 2em;
              // padding: 1em .5em;
            }
            border-bottom: 1px solid #ededed;
          }
          .page{
            padding: 1em .5em;
            text-align: center;
          }
          .nodata{
            color: red;
            text-align: center;
            padding: 1em .5em;
          }
          .artitle{
            cursor: pointer;
          }
        }
        .rank{
          position: relative;
          width: 200px;
          height: 300px;
          .hot{
            position: absolute;
            top: -30px;
            left: 0;
            width: 95px;
            height: 60px;
            background-color: #ff7800;
            color: white;
            font-size: 20px;
            font-weight: bold;
            text-align: center;
            line-height: 60px;
            border-radius: 5px;
          }
          .rank_list{
            margin-top: 20px;
            li{
              height: 20px;
              border-bottom: 1px solid #ededed;
              padding: 20px 0;
              line-height: 20px;
              text-overflow: ellipsis; //超出文本省略号显示
              white-space: nowrap; //不允许换行
              overflow: hidden;
              cursor: pointer;
            }
          }
        }
      }
      
      .title{
        font-size: 20px;
        font-weight: bold;
        color: rgb(29, 166, 186);
        padding: 1em .5em;
      }
    }
  }
</style>